<?php
//Connection statement
require_once('../../../Connections/cn.php');

// Load the common classes
require_once('../../../includes/common/KT_common.php');

// Load the tNG classes
require_once('../../../includes/tng/tNG.inc.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("../../../");

// Start trigger
$formValidation = new tNG_FormValidation();
$formValidation->addField("rut_persona", true, "text", "", "", "", "Please enter a valid value.");
$formValidation->addField("ava_rut_persona", true, "text", "", "", "", "Please enter a valid value.");
$formValidation->addField("apo_rut_persona", true, "text", "", "", "", "Please enter a valid value.");
$formValidation->addField("anio_matricula", true, "numeric", "", "", "", "Please enter a valid value.");
$formValidation->addField("id_curso", true, "numeric", "", "", "", "Please enter a valid value.");
$formValidation->addField("sem_matricula", true, "numeric", "", "", "", "Please enter a valid value.");
$tNGs->prepareValidation($formValidation);
// End trigger

function semestre(){
	$mes = date('m');
	switch($mes){
		case 01:
		case 02:
		case 03:
		case 04:
		case 05:
		case 06:
			$semestre = 1;
		break;
		case 07:
		case 08:
		case 09:
		case 10:
		case 11:
		case 12:
			$semestre = 2;
	}
	return $semestre;
}


// begin Recordset
$colname__rsCurso = '-1';
if (isset($_POST['curso'])) {
  $colname__rsCurso = $_POST['curso'];
}
$query_rsCurso = sprintf("SELECT id_curso, des_curso FROM curso WHERE id_curso = %s", $colname__rsCurso);
$rsCurso = $cn->SelectLimit($query_rsCurso) or die($cn->ErrorMsg());
$totalRows_rsCurso = $rsCurso->RecordCount();
// end Recordset

// Make an insert transaction instance
$ins_matricula = new tNG_insert($cn);
$tNGs->addTransaction($ins_matricula);
// Register triggers
$ins_matricula->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Insert1");
$ins_matricula->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$ins_matricula->registerTrigger("END", "Trigger_Default_Redirect", 99, "../../../?mod=matricula&acc=cupos&curso={id_curso}&a={anio_matricula}");
// Add columns
$ins_matricula->setTable("matricula");
$ins_matricula->addColumn("rut_persona", "STRING_TYPE", "POST", "rut_persona");
$ins_matricula->addColumn("ava_rut_persona", "STRING_TYPE", "POST", "ava_rut_persona");
$ins_matricula->addColumn("apo_rut_persona", "STRING_TYPE", "POST", "apo_rut_persona");
$ins_matricula->addColumn("anio_matricula", "NUMERIC_TYPE", "POST", "anio_matricula");
$ins_matricula->addColumn("id_curso", "NUMERIC_TYPE", "POST", "id_curso");
$ins_matricula->addColumn("sem_matricula", "NUMERIC_TYPE", "POST", "sem_matricula");
$ins_matricula->setPrimaryKey("rut_persona", "STRING_TYPE");

// Execute all the registered transactions
$tNGs->executeTransactions();

// Get the transaction recordset
$rsmatricula = $tNGs->getRecordset("matricula");
$totalRows_rsmatricula = $rsmatricula->RecordCount();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php //PHP ADODB document - made with PHAkt 3.6.0?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../../../includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
<script src="../../../includes/common/js/base.js" type="text/javascript"></script>
<script src="../../../includes/common/js/utility.js" type="text/javascript"></script>
<script src="../../../includes/skins/style.js" type="text/javascript"></script>
<?php echo $tNGs->displayValidationRules();?>
</head>

<body>
<?php
	echo $tNGs->getErrorMsg();
?>
<form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>">
  <table width="95%" align="center" cellpadding="2" cellspacing="0" class="KT_tngtable">
    <tr>
      <td width="12%" class="KT_th"><label for="rut_persona">Rut alumno:</label></td>
      <td width="88%"><input name="rut_persona" type="text" id="rut_persona" value="<?php echo $_SESSION['rut_alumno_matricula']; ?>" size="12" maxlength="12" readonly="true" />
          <?php echo $tNGs->displayFieldHint("rut_persona");?> <?php echo $tNGs->displayFieldError("matricula", "rut_persona"); ?> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="ava_rut_persona">Rut aval:</label></td>
      <td><input name="ava_rut_persona" type="text" id="ava_rut_persona" value="<?php echo $_SESSION['ses_rut_mat_apoderado']; ?>" size="12" maxlength="12" readonly="true" />
          <?php echo $tNGs->displayFieldHint("ava_rut_persona");?> <?php echo $tNGs->displayFieldError("matricula", "ava_rut_aval"); ?> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="apo_rut_persona">Rut apoderado:</label></td>
      <td><input name="apo_rut_persona" type="text" id="apo_rut_persona" value="<?php echo $_SESSION['ses_rut_mat_apoderado']; ?>" size="12" maxlength="12" readonly="true" />
          <?php echo $tNGs->displayFieldHint("apo_rut_persona");?> <?php echo $tNGs->displayFieldError("matricula", "apo_rut_persona"); ?> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="anio_matricula">A&ntilde;o Matricula:</label></td>
      <td><input name="anio_matricula" type="text" id="anio_matricula" value="<?php echo KT_escapeAttribute($_POST['periodo']); ?>" size="4" maxlength="4" readonly="true" />
          <?php echo $tNGs->displayFieldHint("anio_matricula");?> <?php echo $tNGs->displayFieldError("matricula", "anio_matricula"); ?> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="id_curso">Curso:</label></td>
      <td><input name="mi_id_curso" type="text" id="mi_id_curso" value="<?php echo KT_escapeAttribute($rsCurso->Fields('des_curso')); ?>" size="32" maxlength="50" readonly="true" />
        <input name="id_curso" type="hidden" id="id_curso" value="<?php echo $rsCurso->Fields('id_curso'); ?>">
          <?php echo $tNGs->displayFieldHint("id_curso");?> <?php echo $tNGs->displayFieldError("matricula", "id_curso"); ?> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="sem_matricula">Semestre:</label></td>
      <td><input name="sem_matricula" type="text" id="sem_matricula" value="<?php echo KT_escapeAttribute(semestre()); ?>" size="2" maxlength="2" readonly="true" />
          <?php echo $tNGs->displayFieldHint("sem_matricula");?> <?php echo $tNGs->displayFieldError("matricula", "sem_matricula"); ?> </td>
    </tr>
    <tr class="KT_buttons">
      <td><br /></td>
      <td><div align="left"><span class="KT_field_error">Nota:Esta acci&oacute;n no puede ser desecha<br />
          <br />
          <input type="submit" name="KT_Insert1" id="KT_Insert1" value="Confirmar la matricula" />
      </span></div></td>
    </tr>
  </table>
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
$rsCurso->Close();
?>
